package com.hf.eagle.web.struts;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hf.eagle.domain.Accessory;
import com.hf.eagle.domain.ProductInfo;
import com.hf.eagle.web.base.BaseAction;
import com.hf.eagle.web.base.SysConstant;

public class AlbumAction extends BaseAction {

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return this.index(mapping, form, request, response);
	}

	public ActionForward index(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean dynaBean = (DynaBean) form;
		String product_id = (String) dynaBean.get("product_id");
		String is_recommend = (String) dynaBean.get("is_recommend");
		String type1_id = (String) dynaBean.get("type1_id");
		String type2_id = (String) dynaBean.get("type2_id");

		ProductInfo productInfo = new ProductInfo();
		if (StringUtils.isNotBlank(is_recommend)) {
			productInfo.setIs_recommend(Integer.valueOf(is_recommend));
		}
		productInfo.getMap().put("first_product", product_id);
		productInfo.getMap().put("type1_id", type1_id);
		productInfo.getMap().put("type2_id", type2_id);
		List<ProductInfo> productList = super.getFacade().getProductInfoService().getProductAlbumList(productInfo);
		if (null != productList && productList.size() > 0) {
			for (ProductInfo product_info : productList) {
				Accessory acce = new Accessory();
				acce.getMap().put("mod_id", 3200);
				acce.getMap().put("record_id", product_info.getProduct_id());
				List<Accessory> acceList = super.getFacade().getAccessoryService().getAccessoryList(acce);
				if (null != acceList) {
					for (Accessory accessory : acceList) {
						if (accessory.getFile_level().intValue() == SysConstant.MAIN_IMG) { // 大图
							product_info.setAccessory_main(accessory);
						} else if (accessory.getFile_level().intValue() == SysConstant.SUB_IMG) { // 小图
							product_info.setAccessory_sub(accessory);
						}
					}
				}
			}
		}
		request.setAttribute("productList", productList);

		return new ActionForward("/album.jsp");
	}
}